package com.scalar.chiptrack.adaptor.utac;

import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.HTML;
import javax.swing.text.html.parser.DTD;
import javax.swing.text.html.parser.DocumentParser;
import javax.swing.text.MutableAttributeSet;
import java.io.Reader;

/**
 * User: ChandraSekhar
 * Date: Jul 19, 2007
 * Time: 1:24:42 PM
 */
public class UTACHtmlParser extends HTMLEditorKit.ParserCallback
{

    private String month;
    private String toMonth;
    private String year;
    private String toYear;
    private String day;
    private String toDay;
    private String hour;
    private String toHour;
    private String timestampAttribute;
    private String toTimestampAttribute;

    public void parse(Reader reader) throws Exception
    {

		DTD dtd = DTD.getDTD("HTML");
		DocumentParser documentParser = new DocumentParser (dtd);
        documentParser.parse (reader, this, true /*ignore CharSet*/);

	}

	public void handleSimpleTag(HTML.Tag t, MutableAttributeSet a, int pos)
    {

        if( t == HTML.Tag.SELECT )
        {
            timestampAttribute = "";
            toTimestampAttribute= "";
            HTML.Attribute name = HTML.getAttributeKey("name");

            if ( name != null && a.getAttribute(name) != null&& a.getAttribute(name).toString() != null )
            {
                if ( a.getAttribute(name).toString().indexOf("TIMESTAMP_mth") != -1 || a.getAttribute(name).toString().indexOf("STAGEENDTIME_FROM_mth") != -1 )
                {
                        timestampAttribute = "month";
                }
                else if (  a.getAttribute(name).toString().indexOf("STAGEENDTIME_TO_mth") != -1 )
                {
                      toTimestampAttribute = "month";
                }
                else if ( a.getAttribute(name).toString().indexOf("TIMESTAMP_year") != -1 || a.getAttribute(name).toString().indexOf("STAGEENDTIME_FROM_year") != -1 )
                {
                        timestampAttribute = "year";
                }
                else if (  a.getAttribute(name).toString().indexOf("STAGEENDTIME_TO_year") != -1 )
                {
                      toTimestampAttribute = "year";
                }
                else if ( a.getAttribute(name).toString().indexOf("TIMESTAMP_day") != -1 || a.getAttribute(name).toString().indexOf("STAGEENDTIME_FROM_day") != -1 )
                {
                        timestampAttribute = "day";
                }
                else if (  a.getAttribute(name).toString().indexOf("STAGEENDTIME_TO_day") != -1 )
                {
                      toTimestampAttribute = "day";
                }
                else if ( a.getAttribute(name).toString().indexOf("TIMESTAMP_hour") != -1 || a.getAttribute(name).toString().indexOf("STAGEENDTIME_FROM_hour") != -1 )
                {
                        timestampAttribute = "hour";
                }
                else if (  a.getAttribute(name).toString().indexOf("STAGEENDTIME_TO_hour") != -1 )
                {
                      toTimestampAttribute = "hour";
                }

            }
        }

        if (t == HTML.Tag.OPTION )
        {
            HTML.Attribute selected = HTML.getAttributeKey("selected");
            if ( selected != null && a.getAttribute(selected) != null && a.getAttribute(selected).toString() != null )
            {
               if ("#DEFAULT".equalsIgnoreCase( a.getAttribute(selected).toString() ) )
               {
                  HTML.Attribute value = HTML.getAttributeKey("value");
                  if ( value != null && a.getAttribute(value) != null && a.getAttribute(value).toString() != null )
                  {
                      if ( "month".equalsIgnoreCase( timestampAttribute ) )
                      {
                          setMonth( a.getAttribute(value).toString() );
                      }
                      else if ( "month".equalsIgnoreCase( toTimestampAttribute ) )
                      {
                          setToMonth( a.getAttribute(value).toString() );
                      }
                      else if ( "year".equalsIgnoreCase( timestampAttribute ) )
                      {
                           setYear( a.getAttribute(value).toString() );
                      }
                      else if ( "year".equalsIgnoreCase( toTimestampAttribute ) )
                      {
                          setToYear( a.getAttribute(value).toString() );
                      }
                      else if ( "day".equalsIgnoreCase( timestampAttribute ) )
                      {
                           setDay( a.getAttribute(value).toString() );
                      }
                      else if ( "day".equalsIgnoreCase( toTimestampAttribute ) )
                      {
                          setToDay( a.getAttribute(value).toString() );
                      }
                      else if ( "hour".equalsIgnoreCase( timestampAttribute ) )
                      {
                           setHour( a.getAttribute(value).toString() );
                      }
                      else if ( "hour".equalsIgnoreCase( toTimestampAttribute ) )
                      {
                          setToHour( a.getAttribute(value).toString() );
                      }
                  }
               }
           }
       }
    }

	public void handleText (char[] data, int pos) {
		//String text = new String(data);
	}


    public String getMonth() {
        return month;
    }

    public void setMonth(String month) {
        this.month = month;
    }

    public String getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year = year;
    }

    public String getDay() {
        return day;
    }

    public void setDay(String day) {
        this.day = day;
    }

    public String getHour() {
        return hour;
    }

    public void setHour(String hour) {
        this.hour = hour;
    }

    public String getToMonth() {
        return toMonth;
    }

    public void setToMonth(String toMonth) {
        this.toMonth = toMonth;
    }

    public String getToYear() {
        return toYear;
    }

    public void setToYear(String toYear) {
        this.toYear = toYear;
    }

    public String getToDay() {
        return toDay;
    }

    public void setToDay(String toDay) {
        this.toDay = toDay;
    }

    public String getToHour() {
        return toHour;
    }

    public void setToHour(String toHour) {
        this.toHour = toHour;
    }
}